{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "\n",
    "# NumPy计算逆矩阵\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的目标是计算一个 \\(2 \\times 2\\) 矩阵 \\( K \\) 的逆矩阵 \\( K^{-1} \\)。在数学上，给定一个可逆矩阵 \\( K \\)，它的逆矩阵满足：\n",
    "\n",
    "$$\n",
    "K K^{-1} = K^{-1} K = I\n",
    "$$\n",
    "\n",
    "其中 \\( I \\) 是单位矩阵：\n",
    "\n",
    "$$\n",
    "I = \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "### 1. 定义矩阵 \\( K \\)\n",
    "代码首先定义了矩阵 \\( K \\)：\n",
    "$$\n",
    "K = \\begin{bmatrix} 1 & 0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "这里使用 `numpy` 的 `np.array()` 来创建矩阵，这种方式能够用于高效的数值计算。\n",
    "\n",
    "### 2. 计算矩阵的逆\n",
    "代码使用 `np.linalg.inv(K)` 计算矩阵 \\( K \\) 的逆矩阵。这是 `numpy` 提供的标准方法，适用于任何可逆矩阵。如果矩阵不可逆（即行列式为零），`numpy` 会抛出错误。\n",
    "\n",
    "### 3. 计算逆矩阵\n",
    "矩阵 \\( K \\) 的逆矩阵可以按照标准公式计算。对于 \\( 2 \\times 2 \\) 矩阵：\n",
    "$$\n",
    "K = \\begin{bmatrix} a & b \\\\ c & d \\end{bmatrix}\n",
    "$$\n",
    "如果矩阵可逆，它的逆矩阵公式为：\n",
    "$$\n",
    "K^{-1} = \\frac{1}{\\det(K)} \\begin{bmatrix} d & -b \\\\ -c & a \\end{bmatrix}\n",
    "$$\n",
    "其中行列式：\n",
    "$$\n",
    "\\det(K) = ad - bc\n",
    "$$\n",
    "\n",
    "对于当前矩阵：\n",
    "$$\n",
    "K = \\begin{bmatrix} 1 & 0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "行列式计算如下：\n",
    "$$\n",
    "\\det(K) = (1 \\times 1) - (0.5 \\times 0) = 1\n",
    "$$\n",
    "\n",
    "因此，矩阵 \\( K \\) 是可逆的，其逆矩阵计算如下：\n",
    "$$\n",
    "K^{-1} = \\frac{1}{1} \\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "= \\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "### 4. 代码的数学意义\n",
    "代码利用 `np.linalg.inv(K)` 计算矩阵 \\( K \\) 的逆，并返回：\n",
    "$$\n",
    "K^{-1} = \\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix}\n",
    "$$\n",
    "这意味着如果我们将 \\( K \\) 乘以它的逆矩阵 \\( K^{-1} \\)，就会得到单位矩阵 \\( I \\)：\n",
    "$$\n",
    "K K^{-1} = \\begin{bmatrix} 1 & 0.5 \\\\ 0 & 1 \\end{bmatrix} \n",
    "\\begin{bmatrix} 1 & -0.5 \\\\ 0 & 1 \\end{bmatrix} \n",
    "= \\begin{bmatrix} 1 & 0 \\\\ 0 & 1 \\end{bmatrix} = I\n",
    "$$\n",
    "这验证了矩阵逆的正确性。\n",
    "\n",
    "### 5. 代码的作用\n",
    "这段代码提供了一种简洁的方法来计算可逆矩阵的逆矩阵，并确保计算结果满足数学定义，即 \\( K K^{-1} = I \\)。如果矩阵不可逆，则 `numpy` 会抛出错误，使用户可以避免错误计算。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f33ad30-4825-4db5-a990-29125da82028",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c1352a6-38be-426c-b177-a0ea0aba4ba8",
   "metadata": {},
   "source": [
    "## 定义矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "94564c8c-7b83-46be-9ab5-0ebf5ed5b3b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "K = np.array([[1, 0.5],\n",
    "              [0, 1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1478ce88-e015-4cfb-8d58-53eef4ddb12a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1. , -0.5],\n",
       "       [ 0. ,  1. ]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K_inv = np.linalg.inv(K)\n",
    "K_inv"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d3f2e31-7cc2-42f1-8b63-dc8bb7214ff5",
   "metadata": {},
   "source": [
    "## 检查"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8d5bc6df-d8b7-4ea3-a35c-71783d53d79d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0.],\n",
       "       [0., 1.]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K @ K_inv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "72538639-72ca-45da-b412-28b0df21a71b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0.],\n",
       "       [0., 1.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "K_inv @ K"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "070c3389-8048-43a3-baa7-6666009bce96",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
